import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, OneToMany } from 'typeorm';
import { UserRole } from '../../common/enums';
import { Device } from '../../devices/entities/device.entity';

@Entity('users')
export class User {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ length: 64, unique: true })
  username: string;

  @Column({ name: 'password_hash', length: 128 })
  passwordHash: string;

  @Column({
    type: 'enum',
    enum: UserRole,
    default: UserRole.USER,
  })
  role: UserRole;

  @Column({ name: 'display_name', length: 64, nullable: true })
  displayName: string;

  @Column({ length: 32, nullable: true, unique: true })
  phone: string;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @Column({ name: 'last_login_at', nullable: true })
  lastLoginAt: Date;

  @Column({ name: 'is_active', default: true })
  isActive: boolean;

  @Column({ name: 'deactivation_reason', length: 255, nullable: true })
  deactivationReason: string;

  @OneToMany(() => Device, (device) => device.user)
  devices: Device[];
}
